package com.fang.gateway.controller;

import com.fang.common.common.CommonResult;
import com.fang.common.util.CommonResultUtil;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author Bernie_fang
 * @Date 2024/12/24 22:24
 **/
@RestController
@RefreshScope
public class TestController {

    @GetMapping("/test/permitAll")
    public CommonResult<String> permitAll() {
        return CommonResultUtil.success("这个方法允许任何人访问");
    }

    @GetMapping("/authenticated")
    public CommonResult<String> authenticated() {
        return CommonResultUtil.success("这个方法需要登录才能访问");
    }

//    @Value("${config.info}")
//    private String configInfo;
//
//    @GetMapping("/test/config/info")
//    public String getConfigInfo() {
//        return configInfo;
//    }

    /**
     * 拥有USER角色和sys:user:info权限可以访问
     * @author Sans
     * @createTime 2019/10/2 14:22
     * @return Map<String, Object> 返回数据MAP
     */
    @PreAuthorize("hasRole('ROLE_USER') and hasPermission('/menuList','sys:user:info')")// hasPermission()方法必须传最少两个参数
    @RequestMapping(value = "/menuList", method = RequestMethod.GET)
    public String sysMenuEntity() {
        Map<String, Object> result = new HashMap<>();
        result.put("title", "拥有USER角色和sys:user:info权限可以访问");
        result.put("data", null);
        return result.toString();
    }

    @PreAuthorize("hasPermission('/testHasPermission','query_user')")// hasPermission()方法必须传最少两个参数
    @RequestMapping(value = "/testHasPermission", method = RequestMethod.GET)
    public String test() {
        Map<String, Object> result = new HashMap<>();
        result.put("title", "拥有USER角色和sys:user:info权限可以访问");
        result.put("data", null);
        return result.toString();
    }

}
